/**
 * FileName: MyBeanUtils
 * Author: TangZaoBang
 * Date: 2023/04/30 11:52
 * Description:
 * <p>
 * History:
 * <author> TangZaoBang
 * <time> 2023/04/30 11:52
 * <version> 1.0.0
 * <desc> 版本描述
 */

package com.bosssoft.quickstart.demo.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;

import java.util.List;

import java.util.function.Supplier;

/**
 * @Class MyBeanUtils
 * @Description 封装BeanUtils方法
 * @Author TangZaoBang
 * @Date 2023/04/30 11:52
 * @Version
 */
public class MyBeanUtils {
    /**
     *  拷贝对象且返回
     * @param source  拷贝源头
     * @param target 目标对象
     * @param <S> 源头类型
     * @param <T> 目标类型
     * @return 返回拷贝完成的对象
     */
    public static <S,T> T copyProperties(S source, Supplier<T> target){
        Assert.notNull(source,"源参数不可以为空");
        T returnTarget=target.get();
        BeanUtils.copyProperties(source,returnTarget);
        return returnTarget;
    }

    public static <S,T> List<T> copyList(List<S> sourceList, Supplier<T> target,IBeanCopyCallback<S,T> beanCopyCallback){
        Assert.notNull(sourceList,"源头列表不能为空");
        List<T> targetList=new ArrayList<>(sourceList.size());
        /**
         * 逐一完成拷贝，如果需要特殊的字段拷贝这在回调方法实现
         */
        sourceList.stream().forEach(element->{
            T tempTarget=target.get();
            BeanUtils.copyProperties(element,tempTarget);
            if(beanCopyCallback!=null){
                beanCopyCallback.execute(element,tempTarget);
            }
            targetList.add(tempTarget);
        });
        return targetList;
    }
    /**
     * @Class MyBeanUtil
     * @Description
     * @Author Administrator
     * @Date 2023-04-03  14:01
     * @version 1.0.0
     */
    interface IBeanCopyCallback<S,T>{
        /**
         *  执行特殊的拷贝
         * @param source 源头对象
         * @param target 目标对象
         */
        void execute(S source,T target);
    }
}
